package com.ruoyi.project.cms.type.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.type.domain.CMSType;
import com.ruoyi.project.cms.type.service.ICMSTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cms/type")
public class CMSTypeController extends BaseController {

    @Autowired
    private ICMSTypeService cmsTypeService;

    //资源地址的公共前缀
    String prefix = "cms/type";

    @RequiresPermissions("cms:type:view")
    @GetMapping
    public String cmsType() {

        return prefix+"/type";
    }

    /**
     * 分页加载公告
     * @return
     */
    @RequiresPermissions("cms:type:list")
    @PostMapping("/list")
    @ResponseBody//设置方法的返回值为响应的数据，ajax响应
    public TableDataInfo list (CMSType cmsType){
        //该方法底层调用PageHelper分页插件，会自动完成分页查询的相关操作
        //底层会自动调用HttpSerletRequest对象获取请求中的分页条件pageNum和pageSize
        startPage();//分页
        List<CMSType> list = cmsTypeService.selectCmsTypeList(cmsType);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(){
        return prefix+"/add";
    }

    @PostMapping("/add")
    @Log(title = "公告类型管理",businessType = BusinessType.INSERT)
    @ResponseBody
    @RequiresPermissions("cms:type:add")
    public AjaxResult add (@Validated CMSType cmsType) {
        if (UserConstants.CMS_TYPE_NAME_NOT_UNIQUE.equals(cmsTypeService.checkCmsTypeNameUnique(cmsType))){
            return error("新增公告类型'" + cmsType.getTypeName() + "'失败，公告类型已存在");
        }
        return toAjax(cmsTypeService.insertCmsType(cmsType));
    }

    /**
     * 检验公告类型名称是否重复
     * @param cmsType
     * @return
     */
    @PostMapping("/checkCMSTypeNameUnique")
    @ResponseBody
    public String checkCMSTypeNameUnique(CMSType cmsType) {
        return cmsTypeService.checkCmsTypeNameUnique(cmsType);
    }

    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        mmap.put("type", cmsTypeService.selectCmsTypeById(typeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存公告类型
     */
    @RequiresPermissions("cms:type:edit")
    @Log(title = "公告类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CMSType cmsType)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(cmsTypeService.checkCmsTypeNameUnique(cmsType)))
        {
            return error("修改公告类型'" + cmsType.getTypeName()+ "'失败，类型名称已存在");
        }
        return toAjax(cmsTypeService.updateCMSType(cmsType));
    }

    /**
     * 删除公告类型
     */
    @RequiresPermissions("cms:type:remove")
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(cmsTypeService.deleteCMSTypeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
